//
//  UserSetPasswordViewController.swift
//  SitPosition
//
//  Created by apple on 2018/12/31.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography
import Toast_Swift

class UserSetPasswordViewController : UserLoginRegistrationBaseViewController {
    
    // MARK: - Properties
    
    // 完成密码设置回调
    var onSettedupBlock: (/*password*/String) -> () = { _ in }
    
    // MARK: - Subviews
    
    // 手机号
    lazy var phoneLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryLightTextColor
        label.font = Theme.Font.boldFont(ofSize: 26)
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    // 新密码
    lazy var newPasswordTextfield: TextFieldWithLRView = {
        let input = _buildTextField(placeholder: Strings.logonNewPasswordPlaceholder,
                                    leftView: UIImageView(image: UIImage(named: "logon_key")))
        input.isSecureTextEntry = true
        return input
    }()
    
    // 完成按钮
    lazy var finishButton: UserLogonStateButton = {
        let button = UserLogonStateButton(title: Strings.logonFinish)
        button.addTarget(self, action: #selector(_step2Finish), for: UIControl.Event.touchUpInside)
        return button
    }()
    
    let seperator1 = UIView.colored(Theme.Color.primaryBlueSeperatorColor)
    
    // MARK: - Object lifecycle
    public var phone: String = "" {
        didSet {
            phoneLabel.text = phone
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        gk_navLeftBarButtonItem = UIBarButtonItem(imageName: "ic_nav_back", target: self, action: #selector(_backButtonTapped))
        
        // 设置Views
        view.addSubviews([
            titleLabel,
            phoneLabel,
            newPasswordTextfield,
            seperator1,
            finishButton
        ])
        _setupContraints()
        
        // 初始化Views
        titleLabel.text = Strings.logonSetPassword
    }
}

// MARK: Private Methods

extension UserSetPasswordViewController {
    
    // 设置视图约束
    func _setupContraints() {
        // titleLabel
        constrain(titleLabel, gk_navigationBar, view) { v, sibling, parent in
            v.top == sibling.bottom + 34
            v.left == parent.left + 40
            v.right == parent.right - 40
            v.height == 25
        }
        
        // phoneLabel
        constrain(phoneLabel, titleLabel) { v, sibling in
            v.top == sibling.bottom + 51
            v.left == sibling.left
            v.right == sibling.right
            v.height == 20
        }
        
        // newPasswordTextfield
        let kTextfieldHeight: CGFloat = 36
        constrain(newPasswordTextfield, phoneLabel) { v, sibling in
            v.top == sibling.bottom + 48
            v.left == sibling.left
            v.height == kTextfieldHeight
            v.right == sibling.right
        }
        
        // seperator1
        constrain(seperator1, newPasswordTextfield) { v, sibling in
            v.top == sibling.bottom+4
            v.left == sibling.left
            v.right == sibling.right
            v.height == 1
        }
        
        // finishButton
        constrain(finishButton, seperator1) { v, sibling in
            v.top == sibling.bottom + 26
            v.left == sibling.left
            v.width == 200
            v.height == 55
        }
    }
    
    // 返回按钮点击事件
    @objc func _backButtonTapped() {
        backCallback()
    }
    
    // 完成
    @objc func _step2Finish() {
        // 验证密码有效性
        guard let password = newPasswordTextfield.text else {
            view.makeToast(Strings.logonPasswordEmpty)
            return
        }
        
        // 完成设置密码
        onSettedupBlock(password)
    }
}
